/*
 * @Description:
 * @Author:
 * @Date: 2024-10-26 11:14:27
 * @LastEditTime: 2024-12-22 22:20:41
 * @LastEditors: XXX
 */
import type { AppState, DeviceType } from "./state";
import { AppMutationTypes } from './mutation-types'
import { setLanguage, setSidebarStatus } from '@/utils/cookies'

export type Mutations<S = AppState> = {
  [AppMutationTypes.TOGGLE_SIDEBAR] (state: S, withoutAnimation: boolean): void
  [AppMutationTypes.CLOSE_SIDEBAR] (state: S, withoutAnimation: boolean): void
  [AppMutationTypes.SET_LANGUAGE] (state: S, language: string): void
  [AppMutationTypes.TOGGLE_DEVICE] (state: S, device: DeviceType): void
  [AppMutationTypes.SET_SIZE] (state: S, size: string): void
}

export const mutations: Mutations = {
  [AppMutationTypes.TOGGLE_SIDEBAR]: function (state: AppState, withoutAnimation: boolean): void {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = withoutAnimation;
    if (state.sidebar.opened) {
      setSidebarStatus('opened');
    } else {
      setSidebarStatus('closed');
    }
  },

  [AppMutationTypes.CLOSE_SIDEBAR]: function (state: AppState, withoutAnimation: boolean): void {
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
    setSidebarStatus('closed');
  },

  [AppMutationTypes.SET_LANGUAGE]: function (state: AppState, language: string): void {
    state.language = language;
    setLanguage(language);
  },

  [AppMutationTypes.TOGGLE_DEVICE] (state: AppState, device: DeviceType) {
    state.device = device;
  },
  [AppMutationTypes.SET_SIZE]: function (state: AppState, size: string): void {
    state.size = size
  }
}
